package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.itheima.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


/**
 * 1.把这个类也加上@Configuration注解,并且添加@ComponentScan("当前类的路径")
 *
 * 2.在SpringConfig中导入
 */
//@Configuration
public class JdbcConfig {

    //简单类型注入:直接
    @Value("com.mysql.jdbc.Driver")
    private String driverName ;
    @Value("hdbc:mysql://localhost:3306/tlias")
    private String url;
    @Value("root")
    private String username;
    @Value("204020")
    private String password;

//    1.定义要管理的第三方对象
//    2.把返回值声明为bean

    //引用类型的注入要写成形参

    //他看到这个形参就会去IOC中找,所以IOC中要先有这个bean,所以要开启组件扫面
    @Bean
    public DataSource dataSource(BookDao bookDao) {

        System.out.println("\n" + bookDao);
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverName);
            dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }

}
